from contents.models import GoodsChannel


def goodscat():
    goods_qs = GoodsChannel.objects.order_by('group_id','sequence')
    categories = {}
    for good_model in goods_qs:
        group_id = good_model.group_id
        if group_id not in categories:
            categories[group_id] = {
                'channels':[],
                'sub_cats':[]
            }
        cat1 = good_model.category
        cat1.url = good_model.url     # 建立一个临时对象属性
        categories[group_id]['channels'].append(cat1)
        cat2 = cat1.subs.all()  #  得到了二级对象
        for cat2_model in cat2:
            cat3 = cat2_model.subs.all()  # 得到了三级对象
            cat2_model.sub_cats = cat3    # 注意此处cat2_model，查询集，是个对象，不能用点
            categories[group_id]['sub_cats'].append(cat2_model)
    return categories